package com.pxx.item.controller;

import com.pxx.common.utils.PageResult;
import com.pxx.common.utils.R;
import com.pxx.item.mapper.TbBrandMapper;
import com.pxx.item.pojo.TbBrand;
import com.pxx.item.service.TbBrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author : Yoo19
 * @description :
 * @createDate : 2025/6/23 19:40 40
 */
@RestController
@RequestMapping("/item/brand")
public class BrandController {
    @Autowired
    private TbBrandService tbBrandService;

    /**
     * 获取品牌分页信息
     * @param page
     * @param rows
     * @param sortBy
     * @param desc
     * @param key
     * @return
     */
    @GetMapping("page")
    public ResponseEntity<PageResult<TbBrand>> queryBrandPage(
            @RequestParam(value = "page",defaultValue = "1") Integer page,
            @RequestParam(value = "rows",defaultValue = "5") Integer rows,
            @RequestParam(value = "sortBy",required = false) String sortBy,
            @RequestParam(value = "desc",defaultValue = "false") Boolean desc,
            @RequestParam(value = "key",required = false) String key
    ){

     PageResult<TbBrand> pages = tbBrandService.queryBrandPage(page,rows,sortBy,desc,key);
        return ResponseEntity.ok(pages);
    }

    /**
     * 添加品牌
     * @param tbBrand
     * @param cids
     * @return
     */
    @PostMapping("/addOrUpdate")
    public ResponseEntity<Void> saveBrand(TbBrand tbBrand, @RequestParam("cids")List<Long> cids){
        tbBrandService.saveBrand(tbBrand,cids);

        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    /**
     * 修改品牌
     * @param tbBrand
     * @param cids
     * @return
     */
    @PutMapping("/addOrUpdate")
    public ResponseEntity<Void> updateBrand(TbBrand tbBrand,@RequestParam("cids")List<Long> cids){
        tbBrandService.updateBrand(tbBrand,cids);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }


    @PostMapping("/delete/{id}")
    public ResponseEntity<Void> deleteBrand(@PathVariable("id")Long id){
        tbBrandService.deleteBrand(id);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @GetMapping("/cid/{cid}")
    public ResponseEntity<List<TbBrand>> queryBrandByCid(@PathVariable("cid")Long cid){
        List<TbBrand> brandList = tbBrandService.queryBrandByCid(cid);
        if(CollectionUtils.isEmpty(brandList)){
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }else {
            return ResponseEntity.ok(brandList);
        }
    }
}
